package com.xw.hchat.controller;

import com.xw.hchat.mapper.TbFriendReqMapper;
import com.xw.hchat.pojo.TbFriendReq;
import com.xw.hchat.pojo.vo.FriendReq;
import com.xw.hchat.pojo.vo.Result;
import com.xw.hchat.pojo.vo.User;
import com.xw.hchat.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName FriendController
 * @Author liu zhiyong
 * @Date 2020/8/19 9:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    /**
     * 发送好友请求
     * @Author liu zhiyong
     * @Date 2020/8/19 10:19
     * @param tbFriendReq
     * @return com.xw.hchat.pojo.vo.Result
     **/
    @RequestMapping("/sendRequest")
    public Result sendRequest(@RequestBody TbFriendReq tbFriendReq){
        try {
            friendService.sendRequest(tbFriendReq.getFromUserid(), tbFriendReq.getToUserid());
            return new Result(true, "已申请");
        }catch(RuntimeException e){
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch(Exception e) {
            e.printStackTrace();
            return new Result(false, "申请好友失败");
        }
    }

    /**
     * 根据用户id，查询他对应的好友请求
     * @Author liu zhiyong
     * @Date 2020/8/19 11:07
     * @param userid
     * @return java.util.List<com.xw.hchat.pojo.vo.FriendReq>
     **/
    @RequestMapping("/findFriendReqByUserid")
    public List<FriendReq> findFriendReqByUserid(String userid){
        return friendService.findFriendReqByUserid(userid);
    }

    /**
     * 通过好友请求
     * @Author liu zhiyong
     * @Date 2020/8/19 10:43
     * @param reqid 
     * @return com.xw.hchat.pojo.vo.Result 
     **/
    @RequestMapping("/acceptFriendReq")
    public Result acceptFriendReq(String reqid){
        try {
            friendService.acceptFriendReq(reqid);
            return new Result(true, "添加好友成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加好友失败");
        }

    }


    /**
     * 忽略好友请求
     * @Author liu zhiyong
     * @Date 2020/8/19 10:43
     * @param reqid
     * @return com.xw.hchat.pojo.vo.Result
     **/
    @RequestMapping("/ignoreFriendReq")
    public Result ignoreFriendReq(String reqid){
        try {
            friendService.ignoreFriendReq(reqid);
            return new Result(true, "忽略好友请求成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "忽略好友请求失败");
        }

    }

    /**
     * 查询好友列表
     * @Author liu zhiyong
     * @Date 2020/8/19 11:28
     * @param userid
     * @return java.util.List<com.xw.hchat.pojo.vo.User>
     **/
    @RequestMapping("/findFriendByUserid")
    public List<User> findFriendByUserid(String userid){
        return friendService.findFriendsByUserid(userid);
    }
}
